const mysql2 = require('mysql2');

// 创建数据库连接
let connection = mysql2.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'abc123456',
  database: 'hhh',
  port: 3306
});
//let sql = `CREATE TABLE IF NOT EXISTS hhh (
 // id INT PRIMARY KEY AUTO_INCREMENT,
 // name VARCHAR(255) NOT NULL,
 // age INT NOT NULL,
 // gender VARCHAR(10) NOT NULL
//)`;
let s = `SELECT * FROM hhh`;
let sql = `INSERT INTO hhh (name,age,gender) VALUES ('张三',18,'男')`;

//插入内容
connection.query(sql, function(err, results, fields) {
  if (err) {
    console.log(err);
  } else {
    console.log('插入的数据为：', results);
    
    // 添加查询语句来检索刚刚插入的数据
    let querySql = `SELECT * FROM hhh WHERE id = ${results.insertId}`;
    connection.query(querySql, function(err, queryResults, fields) {
      if (err) {
        console.log(err);
      } else {
        console.log('查询到的数据为：', queryResults);
      }
      // 关闭连接
      connection.end();
    });
  }
});